package com.sfc.spiderweb.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sfc.spiderweb.entity.LazadaBillData;
import com.sfc.spiderweb.mapper.LazadaBillDataMapper;
import com.sfc.spiderweb.service.LazadaBillDataSerive;
import com.sfc.spiderweb.util.DateUtil;
import com.sfc.spiderweb.util.ExcelUtils;
import com.sfc.spiderweb.util.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.File;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
@Slf4j
public class LazadaBillDataSeriveImpl extends ServiceImpl<LazadaBillDataMapper, LazadaBillData> implements LazadaBillDataSerive {

    @Autowired
    private LazadaBillDataMapper lazadaBillDataMapper;

    //按每3个一组分割
    private static final Integer MAX_NUMBER = 1000;

    @Override
    public void addDate(String filePath, String filePath2) throws Exception {
        File file = new File(filePath);
        File[] files = file.listFiles();
        for (File file1 : files) {
            if (file1.isDirectory()) {
                File[] files1 = file1.listFiles();
                for (File file2 : files1) {
                    String fileName = file2.getName();
                    String billDate = fileName.substring(fileName.lastIndexOf("-") + 1, fileName.lastIndexOf("."));
                    insert(file2.getAbsolutePath(), billDate, 1);
                }
            }
            String fileNmae = file1.getName();
            String billDate = fileNmae.substring(fileNmae.lastIndexOf("-") + 1, fileNmae.lastIndexOf("."));
            insert(file1.getAbsolutePath(), billDate, 1);
        }
        insert(filePath2, "202005", 2);
    }

    private void insert(String absolutePath, String billDate, Integer type) throws Exception {
        List<LazadaBillData> lazadaBillDataList = new ArrayList<>();
        if (1 == type) {
            List<List<String>> lists = FileUtil.readCsv(absolutePath);
            List<List<String>> lists1 = lists.subList(1, lists.size());
            for (List<String> list : lists1) {
                LazadaBillData lazadaBillData = new LazadaBillData();
                lazadaBillData.setAccount(list.get(0));
                lazadaBillData.setBillDate(billDate);
                lazadaBillData.setBillType(type);
                lazadaBillData.setDeductionTime(StringUtils.isEmpty(list.get(1)) ? null : DateUtil.parse(list.get(1), "yyyy-MM-dd HH:mm:ss"));
                lazadaBillData.setTransactionDate(list.get(2));
                lazadaBillData.setTransactionType(list.get(3));
                lazadaBillData.setFeeName(list.get(4));
                lazadaBillData.setTransactionNumber(list.get(5));
                lazadaBillData.setDetails(list.get(6));
                lazadaBillData.setSellerSku(list.get(7));
                lazadaBillData.setLazadaSku(list.get(8));
                lazadaBillData.setAmount(new BigDecimal(list.get(9)));
                lazadaBillData.setVatInAmount(new BigDecimal(list.get(10)));
                lazadaBillData.setWhtAmount(new BigDecimal(list.get(11)));
                lazadaBillData.setWhtIncludedInAmount(list.get(12));
                lazadaBillData.setStatement(list.get(13));
                lazadaBillData.setPaidStatus(list.get(14));
                lazadaBillData.setOrderNo(list.get(15));
                lazadaBillData.setOrderItemNo(list.get(16));
                lazadaBillData.setOrderItemStatus(list.get(17));
                lazadaBillData.setShippingProvider(list.get(18));
                lazadaBillData.setShippingSpeed(list.get(19));
                lazadaBillData.setShipmentType(list.get(20));
                lazadaBillData.setReference(list.get(21));
                lazadaBillData.setComment(list.get(22));
                lazadaBillData.setPaymentRefId(list.get(23));
                lazadaBillDataList.add(lazadaBillData);
            }
        } else {
            File file = new File(absolutePath);
            File[] files = file.listFiles();
            for (File file1 : files) {
                if (!file1.isDirectory() && file1.getName().endsWith(".xlsx")) {
                    List<List<String>> lists = ExcelUtils.readExcel(file1.getAbsolutePath(), 0, 1);
                    for (List<String> list : lists) {
                        LazadaBillData lazadaBillData = new LazadaBillData();
                        lazadaBillData.setAccount(list.get(0));
                        lazadaBillData.setBillDate(billDate);
                        lazadaBillData.setOrderNo(list.get(1));
                        lazadaBillData.setBillType(type);
                        lazadaBillData.setAmount(new BigDecimal(list.get(2)));
                        lazadaBillData.setStatement("应收余额");
                        lazadaBillDataList.add(lazadaBillData);
                    }
                }
            }
        }
        //太慢
        //this.saveBatch(lazadaBillDataList);
        int limit = countStep(lazadaBillDataList.size());
        //使用流遍历分割list
        List<List<LazadaBillData>> mglist = new ArrayList<>();
        Stream.iterate(0, n -> n + 1).limit(limit).forEach(i -> {
            mglist.add(lazadaBillDataList.stream().skip(i * MAX_NUMBER).limit(MAX_NUMBER).collect(Collectors.toList()));
        });
        for (List<LazadaBillData> lazadaBillData : mglist) {
            lazadaBillDataMapper.insertBatch(lazadaBillData);
        }
    }

    /**
     * 计算切分次数
     */
    private static Integer countStep(Integer size) {
        return (size + MAX_NUMBER - 1) / MAX_NUMBER;
    }
}
